﻿//Write a program that compares two char arrays lexicographically (letter by letter).


using System;

namespace CompareCharArray
{
    class CompareCharArray
    {
        static void Main()
        {
            int arr1, arr2;

            Console.Write("Enter CharLenght1 = ");
            arr1 = int.Parse(Console.ReadLine());
            char[] charArr1 = new char[arr1];                       //intiliaze charArr1[arr1]

            for (int i = 0; i < arr1; i++)
            {
                Console.Write("Enter char for charArr1[{0}] = ", i);
                charArr1[i] = char.Parse(Console.ReadLine());       //fill the array
            }

            Console.Write("Enter CharLenght2 = ");
            arr2 = int.Parse(Console.ReadLine());
            char[] charArr2 = new char[arr2];                       //intiliaze charArr2[arr2]

            for (int i = 0; i < arr2; i++)
            {
                Console.Write("Enter char for charArr2[{0}] = ", i);
                charArr2[i] = char.Parse(Console.ReadLine());       //fill the array
            }

            string[] result = { "First Array", "Second Array", "Equal" }; //create result string array

            int index = 0, minLenght = 0;       //intiliaze index for the result & min lenght for the loop

            if (charArr1.Length > charArr2.Length)  //check if charArr1 lenght is longer
            {
                index = 1;
                minLenght = charArr2.Length;
            }
            else if (charArr1.Length < charArr2.Length)  //check if charArr2 lenght is longer
            {
                index = 0;
                minLenght = charArr1.Length;
            }
            else if (charArr1.Length == charArr2.Length)    //check if they are euqaul by lenght
            {
                index = 2;
                minLenght = charArr1.Length;
            }

            for (int i = 0; i < minLenght; i++) //compare the charArrays indexes
            {
                if (charArr1[i] == charArr2[i])
                {
                    index = 2;
                }
                else if (charArr1[i] > charArr2[i])
                {
                    index = 1;
                    break;
                }
                else if (charArr1[i] < charArr2[i])
                {
                    index = 0;
                    break;
                }
            }
            Console.WriteLine("\n\rResult: {0}\n\r", result[index]); //print the result
        }
    }
}
